package com.wxy.school.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wxy.school.common.R;
import com.wxy.school.dto.JobDto;
import com.wxy.school.entity.Job;
import com.wxy.school.service.JobService;
import com.wxy.school.utils.BaseContext;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 王祥宇
 * @projectName school
 * @packageName com.wxy.school.controller
 * @data 2023/3/7 14:22
 * @description: TODO
 * @version: 1.0
 */

@RestController
@RequestMapping("school/job")
@Slf4j
public class JobController {

    @Resource
    private JobService jobService;

    /**
     * @description: 获取job的所有数据信息
     * @author: 王祥宇
     * @param: pageNum 查询的页数
     * @param: pageSize 每页查询数据条数
     * @param: qurey 模糊查询参数
     * @return: com.wxy.school.common.R<com.baomidou.mybatisplus.extension.plugins.pagination.Page>
     */
    @GetMapping("/getJobList")
    public R<Page> get(int pageNum, int pageSize, String qurey){
        //创建分页查询构造器
        Page<Job> jobPage = new Page<>(pageNum, pageSize);
        //创建查询条件
        LambdaQueryWrapper<Job> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //参数qurey不为空时添加该条件
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(qurey), Job::getJobName, qurey);
        //排序查询
        lambdaQueryWrapper.orderByDesc(Job::getUpdateTime);
        //执行查询
        jobService.page(jobPage, lambdaQueryWrapper);
        //返回查询结果
        return R.success(jobPage);
    }

    /**
     * @description: 发布兼职信息
     * @author: 王祥宇
     * @param: jobDto
     * @return: com.wxy.school.common.R<java.lang.String>
     */
    @PutMapping("/addJob")
    public R<String> add(@RequestBody JobDto jobDto){
        Job job = new Job();
        //拷贝数据
        BeanUtils.copyProperties(jobDto, job, "userName");
        //设置线程
        BaseContext.setThreadLocal(jobDto.getUserName());
        //执行添加操作
        jobService.save(job);
        //返回结果
        return R.success("兼职发布成功");
    }

    /**
     * @description: 修改兼职信息
     * @author: 王祥宇
     * @param: jobDto
     * @return: com.wxy.school.common.R<java.lang.String>
     */
    @PutMapping("/updateJob")
    public R<String> update(@RequestBody JobDto jobDto){
        Job job = new Job();
        //拷贝数据
        BeanUtils.copyProperties(jobDto, job, "userName");
        //设置线程
        BaseContext.setThreadLocal(jobDto.getUserName());
        //执行更新操作
        jobService.updateById(job);
        //返回结果
        return R.success("兼职信息修改成功");
    }

    /**
     * @description: 删除兼职信息
     * @author: 王祥宇
     * @param: id
     * @return: com.wxy.school.common.R<java.lang.String>
     */
    @DeleteMapping("/deleteJob/{id}")
    public R<String> delete(@PathVariable Long id){
        jobService.removeById(id);
        return R.success("兼职信息删除成功");
    }

    /**
     * @description: 获取所有公告信息
     * @author: 王祥宇
     * @return: com.wxy.school.common.R<java.util.List<com.wxy.school.entity.Notice>>
     */
    @GetMapping("/getAllJobList")
    public R<List<Job>> getAll(){
        LambdaQueryWrapper<Job> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByDesc(Job::getCreateTime);
        List<Job> jobList = jobService.list(lambdaQueryWrapper);
        return R.success(jobList);
    }

    /**
     * @description: 根据id获取兼职信息
     * @author: 王祥宇
     * @param: noticeId
     * @return: com.wxy.school.common.R<com.wxy.school.entity.Notice>
     */
    @GetMapping("/getById/{jobId}")
    public R<Job> getById(@PathVariable Long jobId){
        Job job = jobService.getById(jobId);
        return R.success(job);
    }

}
